package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.BorrowLine;
import com.zyd.shiro.business.service.PakBorrowLineService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakBorrowLine;
import com.zyd.shiro.persistence.mapper.PakBorrowLineMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakBorrowLineServiceImpl implements PakBorrowLineService {

    @Autowired
    private PakBorrowLineMapper pakBorrowLineMapper;

    @Override
    public BorrowLine insert(BorrowLine borrowLine) {
        Assert.notNull(borrowLine, "对象不可为空！");
        borrowLine.getPakBorrowLine().setBostatus(0);
        borrowLine.getPakBorrowLine().setUpdateTime(new Date());
        borrowLine.getPakBorrowLine().setCreateTime(new Date());
        pakBorrowLineMapper.insertSelective(borrowLine.getPakBorrowLine());
        return borrowLine;
    }

    @Override
    public void insertList(List<BorrowLine> borrowLines) {
            for(BorrowLine borrowLine:borrowLines){
               borrowLine.getPakBorrowLine().setUpdateTime(new Date());
               borrowLine.getPakBorrowLine().setCreateTime(new Date());
               pakBorrowLineMapper.insertSelective(borrowLine.getPakBorrowLine());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakBorrowLineMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(BorrowLine borrowLine) {
        Assert.notNull(borrowLine, "对象不可为空！");
        borrowLine.setUpdateTime(new Date());
        return pakBorrowLineMapper.updateByPrimaryKey(borrowLine.getPakBorrowLine()) > 0;
    }

    @Override
    public boolean updateSelective(BorrowLine borrowLine) {
        Assert.notNull(borrowLine, "对象不可为空！");
        borrowLine.setUpdateTime(new Date());
        return pakBorrowLineMapper.updateByPrimaryKeySelective(borrowLine.getPakBorrowLine()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public BorrowLine getByPrimaryKey(Long primaryKey) {
        PakBorrowLine pakBorrowLine = pakBorrowLineMapper.selectByPrimaryKey(primaryKey);
        return null == pakBorrowLine ? null : new BorrowLine(pakBorrowLine);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public BorrowLine getOneByEntity(BorrowLine test) {
        Assert.notNull(test, "对象不可为空！");
        PakBorrowLine pakBorrowLine = pakBorrowLineMapper.selectOne(test.getPakBorrowLine());
        return null == pakBorrowLine ? null : new BorrowLine(pakBorrowLine);
    }

    @Override
    public List<BorrowLine> listAll() {
        List<PakBorrowLine> pakBorrowLines = pakBorrowLineMapper.selectAll();
        if (CollectionUtils.isEmpty(pakBorrowLines)) {
            return null;
        }
        List<BorrowLine> borrowLines = new ArrayList<>();
        for (PakBorrowLine pakBorrowLine : pakBorrowLines) {
            borrowLines.add(new BorrowLine(pakBorrowLine));
        }
        return borrowLines;
    }

    @Override
    public List<BorrowLine> listByEntity(BorrowLine entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<BorrowLine> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakBorrowLine> pakBorrowLines = pakBorrowLineMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakBorrowLines)) {
            return null;
        }
        List<BorrowLine> borrowLines = new ArrayList<>();
        for (PakBorrowLine pakBorrowLine : pakBorrowLines) {
            borrowLines.add(new BorrowLine(pakBorrowLine));
        }
        PageInfo bean = new PageInfo<PakBorrowLine>(pakBorrowLines);
        bean.setList(borrowLines);
        return bean;
    }
@Override
    public void increase(PakBorrowLine pakBorrowLine) {
        pakBorrowLineMapper.insert(pakBorrowLine);
    }

    @Override
    public List<PakBorrowLine> findByBorrow(Long borrowId) {
        return pakBorrowLineMapper.findByBorrow(borrowId);
    }


}
